package vcp.server;

import java.util.HashMap;

import vcp.server.DAO.ComplaintDAO;
import vcp.server.DAO.ComplaintHistoryDAO;
import vcp.server.DAO.DayStatisticsDAO;
import vcp.server.DAO.PeriodicReportDAO;
import vcp.server.DAO.SiteCostUpdatesDAO;
import vcp.server.DAO.SubscriptionDAO;
import vcp.server.DAO.SubscriptionHistoryDAO;
import vcp.server.DAO.WeekStatisticsDAO;
import vcp.server.DAO.interfaces.IGenericDAO;
import vcp.server.utility.Constants.TableNames;

/**
 * 
 * @author VCP team
 *         <p>
 *         Responsible for auto generating IDs for DB tables
 *         </p>
 */
public class DBTablesIDGenerator
{
	/** reference to map which hold next generated ID per table **/
	private final HashMap <String, Integer> generatedIDsMap;

	public DBTablesIDGenerator ()
	{
		this.generatedIDsMap = new HashMap <>();
	}

	/**
	 * 
	 * @param tableName
	 *            for id to be generated
	 * @return new generated ID
	 */
	public Integer generateID ( String tableName )
	{
		// lazy initialization for map's key value
		if ( ! this.generatedIDsMap.containsKey(tableName) )
		{
			this.generatedIDsMap.put(tableName, this.findID(tableName));
		}

		Integer generatedID = this.generatedIDsMap.get(tableName);
		this.generatedIDsMap.put(tableName, generatedID + 1);

		return generatedID;
	}

	/**
	 * 
	 * @param tableName
	 *            for id to be generated
	 * @return new generated ID
	 */
	private Integer findID ( String tableName )
	{
		IGenericDAO <?, ?> dao = null;
		switch ( tableName )
		{
			case TableNames.SUBSCRIPTION:
				dao = new SubscriptionDAO();
				break;

			case TableNames.COMPLAINT:
				dao = new ComplaintDAO();
				break;

			case TableNames.COMPLAINT_HISTORY:
				dao = new ComplaintHistoryDAO();
				break;

			case TableNames.SUBSCRIPTIONHISTORY:
				dao = new SubscriptionHistoryDAO();
				break;

			case TableNames.SITE_COSTS_UPDATE:
				dao = new SiteCostUpdatesDAO();
				break;
			case TableNames.PERIODIC_REPORT:
				dao = new PeriodicReportDAO();
				break;
			case TableNames.DAY_STATISTICS:
				dao = new DayStatisticsDAO();
				break;
			case TableNames.WEEKLY_STATISTICS:
				dao = new WeekStatisticsDAO();
				break;
		}

		return dao.findMaxID();
	}
}
